package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  检查项服务
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    //添加检查项
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    //分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //当前页
        Integer currentPage = queryPageBean.getCurrentPage();
        //每页记录数
        Integer pageSize = queryPageBean.getPageSize();
        //分页条件
        String queryString = queryPageBean.getQueryString();

        //使用PageHelper完成分页
        PageHelper.startPage(currentPage, pageSize);

        //调用dao
        Page<CheckItem> page = checkItemDao.selectByCondition(queryString);

        //获取总记录数
        long total = page.getTotal();
        //获取数据集合
        List<CheckItem> rows = page.getResult();

        return new PageResult(total, rows);
    }

    //删除检查项
    public void deleteById(Integer id) {
        long count = checkItemDao.findCountByCheckItemId(id);
        if(count > 0){
            //检查项关联检查组，不允许删除
            new RuntimeException();
        }
        checkItemDao.deleteById(id);
    }

    //编辑检查项
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    //根据id查找检查项
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    //查找所有检查项
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
}
